home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / dynamics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  7.4 KB  |  200 lines

  1. #ifndef    lint
  2. static char SccsId[]= "@(#)dynamics.c    V1.18    3/13/95";
  3. #endif
  4. /*
  5. |    file name -- dynamics.c
  6. |===================================================================
  7. |
  8. |                      V.I. Corporation
  9. |                       Copyright (c) 1990
  10. |
  11. |  This program demonstrates the use of DVtools dynamics.  It creates
  12. |  a simple dial, rotating inside a circle.  The rotation and
  13. |  foreground color of the dial are controlled by one dynamic control
  14. |  object.  The rotation varies from 170 to -170 degrees, rotating
  15. |  around the center of the circle.  When the dial is in the first
  16. |  third of the rotation, it is green; in the second third, it is
  17. |  yellow; in the last third, it is red. The output value of an angle
  18. |  is displayed using the text object controlled by another dynamic
  19. |  control object.
  20. |
  21. |  There is no use of the data source facility. Two VDPs are created,
  22. |  using our own internal variables. These variables are  manipulated
  23. |  directly.
  24. |
  25. |  The source of data for a dial is generated by combining the results of a
  26. |  sine and cosine curve.  This causes the dial to rotate back
  27. |  and forth, in a varying flowing pattern. The source of data for a text
  28. |  object is generated by mapping the input value for a dial to an output
  29. |  rotation value, based on the ranges for VDPs.
  30. |
  31. |  The program will exit automatically after the loop has completed.
  32. |
  33. |===================================================================
  34. */
  35. #include<windows.h>
  36.  
  37. #include <math.h>
  38. #include "std.h"
  39. #include "dvstd.h"
  40. #include "dvtools.h"
  41. #include "VOstd.h"
  42. #include "Tfundecl.h"
  43. #include "VOfundecl.h"
  44. #include "VPfundecl.h"
  45.  
  46.  
  47. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  48.                      LPSTR lpCmdLine,  int nCmdShow  )
  49. {
  50.   INT argc = 0;
  51.   CHAR **argv;
  52.    
  53.   OBJECT screen;
  54.   VIEW view;
  55.   DRAWPORT drawport;
  56.   OBJECT drawing, dial, border, text_object, anchor_point;
  57.   OBJECT dyn_control_dial, dyn_control_text;
  58.   OBJECT rotation_vd, color_vd, color_tt, text_vd;
  59.   OBJECT center_point, dial_point, border_point;
  60.   float dial_input = 0.0, text_input = 0.0, low_range_out, high_range_out;
  61.   double low_range_in, high_range_in;
  62.   VARDESC dial_rotation_vdp, dial_color_vdp, text_input_vdp;
  63.   ATTRIBUTES text_attr;
  64.   int n;
  65.  
  66.   /* Initialize DV-Tools - use config var DVPATH */
  67.   make_argv(&argc,&argv,GetCommandLine());
  68.   TInit ((char *) NULL, (char *) NULL);
  69.  
  70.   /* Open the requested graphics device */
  71.   if (argc > 1)
  72.     screen = TscOpenSet (argv[1], (char *) NULL,
  73.                          V_X_EXPOSURE_BLOCK, YES, V_END_OF_LIST);
  74.  
  75.   /* Open the default graphics device defined in DataViews
  76.      Configuration file */
  77.   else
  78.     screen = TscOpenSet ((char *) NULL, (char *) NULL,
  79.                          V_X_EXPOSURE_BLOCK, YES, V_END_OF_LIST);
  80.  
  81.   /* Unable to open device - notify user and exit */
  82.   if (!screen)
  83.     {
  84.        printf ("Must specify device on command line or");
  85.        printf (" in DataViews configuration file.\n");
  86.       exit (EXIT_ERR);
  87.     }
  88.  
  89.   /* Create a new, empty view */
  90.   view = TviCreate ();
  91.  
  92.   /* Create a drawport using the view */
  93.   drawport = TdpCreate (screen, view, (RECTANGLE *)NULL, (RECTANGLE *)NULL);
  94.  
  95.   /* Get the empty drawing from the view */
  96.   drawing = TviGetDrawing (view);
  97.  
  98.   /* Create point objects to define graphical objects */
  99.   center_point = VOptCreate (WORLD_COORDINATES, 0, 0, (OBJECT) NULL);
  100.   dial_point = VOptCreate (WORLD_COORDINATES, 0, 4000, (OBJECT) NULL);
  101.   border_point = VOptCreate (WORLD_COORDINATES, 0, 4200, (OBJECT) NULL);
  102.  
  103.   /* Create a line dial and a circle border */
  104.   dial = VOlnCreate (center_point, dial_point, (ATTRIBUTES *) NULL);
  105.   border = VOciCreate (center_point, border_point, (ATTRIBUTES *) NULL);
  106.  
  107.   /* Create a  text object to display output value of a dynamic action.  */
  108.   anchor_point = VOptCreate (WORLD_COORDINATES, 0, -8000, (OBJECT) NULL);
  109.   VOuAtInit (&text_attr);
  110.   text_attr.foreground_color = VOcoCreate (COLOR_COMPONENTS, 255, 0, 0);  /*red*/
  111.   text_object = VOtxCreate (" ", anchor_point, &text_attr);
  112.  
  113.   /* add created objects to a drawing */
  114.   VOdrObAdd (drawing, dial);
  115.   VOdrObAdd (drawing, border);
  116.   VOdrObAdd (drawing, text_object);
  117.  
  118.   /* Create vdp's to control the dial input, set the ranges */
  119.   low_range_in = -2.0;
  120.   high_range_in = 2.0;
  121.   dial_rotation_vdp = VPvdcreate ((ADDRESS) & dial_input, V_F_TYPE);
  122.   VPvd_drange (dial_rotation_vdp, low_range_in, high_range_in);
  123.   dial_color_vdp = VPvdcreate ((ADDRESS) & dial_input, V_F_TYPE);
  124.   VPvd_drange (dial_color_vdp, low_range_in, high_range_in);
  125.  
  126.   /* Create VD objects, for rotation and color change */
  127.   rotation_vd = VOvdCreate (dial_rotation_vdp, NUMBER, (DATUM) 0.0);
  128.   color_vd = VOvdCreate (dial_color_vdp, NUMBER, (DATUM) 0.0);
  129.  
  130.   /* Create a threshold table for color change, defaulting to green */
  131.   color_tt = VOttCreate (color_vd, OBJECT_DATUM (OT_COLOR),
  132.                          VOcoCreate (COLOR_NAME, (LONG) "green"));
  133.  
  134.   /* Add the thresholds to the table - values must be normalized */
  135.   VOttAddThresh (color_tt, (int) (32767 * 0.6666667),
  136.                  VOcoCreate (COLOR_NAME, (LONG) "red"));
  137.   VOttAddThresh (color_tt, (int) (32767 * 0.3333333),
  138.                  VOcoCreate (COLOR_NAME, (LONG) "yellow"));
  139.  
  140.   /* Create a dynamic control object for the dial and set the erase
  141.      method for speed */
  142.   dyn_control_dial = VOdyCreate ();
  143.   VOdySetEraseMethod (dyn_control_dial, V_DYN_ERASE_BGCLR);
  144.  
  145.   /* Set the range of angles - the smaller "high" range enables a clockwise
  146.       rotation as the value of dial_input increases */
  147.   low_range_out = 170.0;
  148.   high_range_out = -170.0;
  149.  
  150.   /* Attach the two data objects to the dial dynamic control object */
  151.   VOdyAttachData (dyn_control_dial, V_DYN_ROTATE, rotation_vd, &low_range_out,
  152.                   &high_range_out, center_point, (OBJECT) NULL);
  153.   VOdyAttachData (dyn_control_dial, FOREGROUND_COLOR, color_tt, (float *) NULL,
  154.                   (float *) NULL, (OBJECT) NULL, (OBJECT) NULL);
  155.  
  156.   /* Attach the dynamic control object to the dial */
  157.   VOobDySet (dial, dyn_control_dial);
  158.  
  159.   /* Create a vdp to control the text_object input */
  160.   text_input_vdp = VPvdcreate ((ADDRESS) & text_input, V_F_TYPE);
  161.  
  162.   /* Create a VD object for text dynamics */
  163.   text_vd = VOvdCreate (text_input_vdp, NUMBER, (DATUM) 0.0);
  164.  
  165.   /* create a dynamic control object for the text object */
  166.   dyn_control_text = VOdyCreate ();
  167.  
  168.   /* Attach the text data object to the text dynamic control object */
  169.   VOdyAttachData (dyn_control_text, V_DYN_TEXT, text_vd, (float *) NULL,
  170.                   (float *) NULL, (OBJECT) NULL, (OBJECT) NULL);
  171.  
  172.   /* Specify the format string to be used for the text dynamics */
  173.   VOdySetTextFormat (dyn_control_text, V_DYN_TEXT, "rotation value = %8.2f");
  174.  
  175.   /* Attach the dynamic control object to the text object */
  176.   VOobDySet (text_object, dyn_control_text);
  177.  
  178.   /* Draw the objects */
  179.   TdpDraw (drawport);
  180.  
  181.   /* Loop: change the dial input,text input and update the dynamic objects */
  182.   for (n = 0; n < 2000; n++)
  183.     {
  184.       dial_input = sin ((double) (n / 15.0)) - cos ((double) (n / 25.0));
  185.  
  186.       /* Get a mapped output value for the angle */
  187.       text_input = dial_input * (high_range_out - low_range_out) /
  188.         (high_range_in - low_range_in);
  189.  
  190.       TdpDrawNext (drawport);
  191.     }
  192.  
  193.   /* Exit gracefully */
  194.   TdpDestroy (drawport);
  195.   TviDestroy (view);
  196.   TscClose (screen);
  197.   TTerminate ();
  198.   return EXIT_OK;
  199. }
  200.